package org.wang.generalcode.support.utils;

import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.Assert;

import java.lang.reflect.Constructor;

public class BeanUtil extends BeanUtils {
    public BeanUtil() {
    }

    public static <T> T newInstance(Class<?> clazz) {
        return (T) instantiateClass(clazz);
    }

    public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
        Assert.notNull(clazz, "Class must not be null");
        if (clazz.isInterface()) {
            throw new BeanInstantiationException(clazz, "Specified class is an interface");
        } else {
            try {
                return instantiateClass(clazz.getDeclaredConstructor());
            } catch (NoSuchMethodException var3) {
                NoSuchMethodException ex = var3;
                Constructor<T> ctor = findPrimaryConstructor(clazz);
                if (ctor != null) {
                    return instantiateClass(ctor);
                } else {
                    throw new BeanInstantiationException(clazz, "No default constructor found", ex);
                }
            } catch (LinkageError var4) {
                LinkageError err = var4;
                throw new BeanInstantiationException(clazz, "Unresolvable class definition", err);
            }
        }
    }
}
